using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEngine.UI;

public class DragGameMgr : MonoBehaviour
{
    public static DragGameMgr Ins;
    void Awake()
    {
        Ins = this;
    }
    public GameObject player, Drag;
    public PlayableDirector director;
    public Vector3 pos, CameraposStart, CameraposStartLook;
    public Transform CameraposEnd, aimImage;
    public Slider slider, MinSlider;
    public Text numText, perfectText;
    public bool IsOnClick = true, IsStartBut = false, b = true, IsAim, Ainb = true;
    public int dragNum;

    // Start is called before the first frame update
    void Start()
    {
        CameraposStart = Camera.main.transform.position;
        CameraposStartLook = Camera.main.transform.eulerAngles;
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetMouseButtonDown(0) && IsOnClick && IsStartBut)
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
            {
                if (hit.collider)
                {
                    pos = hit.point;

                    IsAim = true;

                    aimImage.gameObject.SetActive(true);

                    aimImage.transform.position = Camera.main.WorldToScreenPoint(pos);
                }
            }
        }
        if (IsStartBut)
        {
            if (b)
            {
                slider.value += Time.deltaTime / 2;
                if (slider.value >= 1)
                {
                    b = false;
                }
            }
            else
            {
                slider.value -= Time.deltaTime / 2;
                if (slider.value <= 0)
                {
                    b = true;
                }
            }
        }
        if (IsAim)
        {
            if (Ainb)
            {
                aimImage.transform.localScale += Vector3.one * Time.deltaTime;
                if (aimImage.transform.localScale.x >= 1)
                {
                    Ainb = false;
                }
            }
            else
            {
                aimImage.transform.localScale -= Vector3.one * Time.deltaTime;
                if (aimImage.transform.localScale.x <= 0.5f)
                {
                    Ainb = true;
                }
            }
        }
        Camera.main.transform.LookAt(Drag.transform.position);
        numText.text = "当前分数：" + dragNum;
    }
    IEnumerator ActionText()
    {
        perfectText.transform.position = slider.handleRect.transform.position;
        perfectText.gameObject.SetActive(true);
        perfectText.fontSize = 0;
        float fontSize = 0;
        while (true)
        {
            fontSize += Time.deltaTime * 80;
            perfectText.fontSize = (int)fontSize;

            if (fontSize >= 80)
            {
                break;
            }
            yield return null;
        }
        perfectText.gameObject.SetActive(false);
    }
    public void StratBut()
    {
        if (!IsStartBut)
        {
            MinSlider.value = Random.Range(0, 1f);

            IsStartBut = true;

            b = true;

            slider.value = 0;

            Camera.main.transform.DOMove(CameraposEnd.position, 0.5f);
        }
    }
    public void fireBut()
    {
        if (aimImage.gameObject.activeSelf)
        {

            IsStartBut = false;
            if (Mathf.Abs(slider.value - MinSlider.value) <= 0.04)
            {
                StartCoroutine("ActionText");
            }
            else
            {
                var offset = Random.insideUnitCircle * Mathf.Abs(slider.value - MinSlider.value);
                pos += new Vector3(offset.x, offset.y, 0);
            }
            Debug.Log("检测到目标");
            aimImage.gameObject.SetActive(false);
            IsOnClick = false;
            director.Play();
            Camera.main.transform.DOMove(CameraposStart, 0.5f);
            Camera.main.transform.DORotate(CameraposStartLook, 0.5f);
        }
        else
        {
            Debug.Log("请选择投掷位置");
        }
    }
}
